package queue;

//                                                  循环队列
//                                                  基于数组实现队列
// 循环队列
public class MyQueueByArray {
    public String[] arr;
    //head 表示队列的头
    public int head = 0;
    //tail 表示队列的尾
    public int tail = 0;
    public int size = 0;
    public MyQueueByArray(){
        arr = new String[1000];
    }
    public MyQueueByArray(int capacity){
        arr = new String[capacity];
    }

    //入队列
    public void offer(String val){
        //判断是否队列满了
        if(size == arr.length){
            return;
        }
        arr[tail] = val;
        tail++;
        if (tail == arr.length){
            tail = 0;
        }
        //实现 tail 循环也可以这样
        //tail = (tail + 1) % arr.length();
        size++;
    }
    //出队列
    public String poll(){
        if (size == 0){
            return null;
        }
        String val = arr[head];
        head++;
        if (head == arr.length){
            head = 0;
        }
        size--;
        return val;
    }
    //取队首元素
    public String peek(){
        return arr[head];
    }
    public boolean isEmpty(){
        return size == 0;
    }
    public int size(){
        return size;
    }
}
